package com.test.platform.dao;

import com.test.platform.model.XTSReport;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface XTSReportMapper {
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, x.* " +
            "FROM xts_report x, (SELECT @rownum := 0) r " +
            "ORDER BY x.id DESC")
    List<XTSReport> findAll();
    
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, x.* " +
            "FROM xts_report x, (SELECT @rownum := 0) r " +
            "WHERE x.id = #{id}")
    XTSReport findById(@Param("id") Integer id);
    
    @Select("SELECT (@rownum := @rownum + 1) AS displayId, x.* " +
            "FROM xts_report x, (SELECT @rownum := 0) r " +
            "WHERE x.owner = #{owner} AND x.name = #{name} " +
            "ORDER BY x.id DESC")
    List<XTSReport> findByOwnerAndName(@Param("owner") String owner, @Param("name") String name);

    @Insert("INSERT INTO xts_report(owner, name, iteration_version, api_version, running_result, " +
            "fail_reason, report_address, created_at, updated_at) " +
            "VALUES(#{owner}, #{name}, #{iterationVersion}, #{apiVersion}, #{runningResult}, " +
            "#{failReason}, #{reportAddress}, #{createdAt}, #{updatedAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(XTSReport xtsReport);

    @Update("UPDATE xts_report SET " +
            "owner = #{owner}, " +
            "name = #{name}, " +
            "iteration_version = #{iterationVersion}, " +
            "api_version = #{apiVersion}, " +
            "running_result = #{runningResult}, " +
            "fail_reason = #{failReason}, " +
            "report_address = #{reportAddress}, " +
            "updated_at = #{updatedAt} " +
            "WHERE id = #{id}")
    int update(XTSReport xtsReport);

    @Delete("DELETE FROM xts_report WHERE id = #{id}")
    int deleteById(@Param("id") Integer id);
}